﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using System.Collections.Specialized;

namespace sales.forms
{
    public partial class voucherAdjustReceive : voucherCashBase
    {
        public voucherAdjustReceive()
        {
            try
            {
                InitializeComponent();
                dbcrType = debitCredit.credit;
                int mask = common.SysLibs.TurnBitOn(0, CommonTypes.Consts.constFlagPos_VoucherDetailCatCode_PAYMENT);
                (myDetailGrid as sales.Controls.gridVoucherDetail).LoadCatCode1(mask);
            }
            catch (Exception er)
            {
                ShowError(er);
            }
        }

        private void LoadCustomerList()
        {
            StringCollection list = new StringCollection(); 
            for (int idx = 0; idx < myApplicationDS.VoucherDetail.Count; idx++)
            {
                if (myApplicationDS.VoucherDetail[idx].RowState == DataRowState.Deleted) continue;
                if (list.Contains(myApplicationDS.VoucherDetail[idx].custCode)) continue;
                if (DataAccess.DataCache.myMasterDS.Customer.FindBycustCode(myApplicationDS.VoucherDetail[idx].custCode)!=null) continue;
                list.Add(myApplicationDS.VoucherDetail[idx].custCode);
            }
            DataAccess.DataLibs.LoadData(DataAccess.DataCache.myMasterDS.Customer, list);
        }
        #region override function
        protected override void SetDetailGrid()
        {
            base.SetDetailGrid();
            this.myDetailGrid.SetColumnVisible(new string[] {
                                                sales.Controls.gridVoucherDetail.ColumnNames.custCode.ToString(), 
                                                sales.Controls.gridVoucherDetail.ColumnNames.amount.ToString(),
                                                sales.Controls.gridVoucherDetail.ColumnNames.custName.ToString()
                                            });
            this.myDetailGrid.myAutoFitColumn = sales.Controls.gridVoucherDetail.ColumnNames.custName.ToString();
            this.myDetailGrid.SetColumnReadOnly(sales.Controls.gridVoucherDetail.ColumnNames.custCode.ToString(), false);
            this.myDetailGrid.DisbaleReadOnlyColumns();
            this.myDetailGrid.AutoArrange();
        }
        protected override bool FillDetailTable(string voucherId)
        {
            bool retVal = base.FillDetailTable(voucherId);
            LoadCustomerList();
            return retVal;
        }
        #endregion
        posDebtOption myPosDebtOption = null;
        private void toolBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (myPosDebtOption == null || myPosDebtOption.IsDisposed)
                {
                    myPosDebtOption = new posDebtOption();
                    myPosDebtOption.myOnProcess += new common.forms.baseDialogForm.onProcess(posDebt_Handler);
                }
                myPosDebtOption.ShowForm();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void posDebt_Handler(object sender, common.myTypes.DialogEvent e)
        {
            this.ShowMessage("");
            if (e.isCloseClick) return;
            e.acceptClose = false;
            myReports.sales.reportDataSet.debtSumTblDataTable tbl = new myReports.sales.reportDataSet.debtSumTblDataTable();
            myReports.SaleDataLibs.OrderValues values = new myReports.SaleDataLibs.OrderValues();
            values.FromDate = myPosDebtOption.myFromDate;
            values.ToDate = myPosDebtOption.myToDate;
            values.SaleOrderType = new string[] { CommonTypes.SaleConsts.constOrderRetail};
            myReports.SaleDataLibs.LoadData(tbl, values);

            data.applicationDS.VoucherDetailRow detailRow;
            myApplicationDS.VoucherDetail.Clear();
            for (int idx = 0; idx < tbl.Count; idx++)
            {
                detailRow = myApplicationDS.VoucherDetail.NewVoucherDetailRow();
                if (tbl[idx].buyAmt == 0) continue;
                DataAccess.DataLibs.InitData(detailRow);
                detailRow.voucherId = voucherIdEd.Text;
                detailRow.custCode = tbl[idx].custCode;
                detailRow.amount = tbl[idx].buyAmt;
                detailRow.currency = tbl[idx].currency;
                myApplicationDS.VoucherDetail.AddVoucherDetailRow(detailRow);
            }
            LoadCustomerList();
            this.ShowMessage("Đã tạo xong số liệu");
        }
        
    }
}
